﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using blackhouse.BlackApart.Business.Opinions;
using blackhouse.Web;
using System.IO;
using System.Globalization;
using blackhouse.BlackApart.Business.Apartments;
using blackhouse.BlackApart.Web;
using blackhouse.Web.Controls.TemplateControl;

namespace blackhouse.Aparts.Web.Modules {
    public partial class OpinionItem : System.Web.UI.UserControl, ITemplateDrop {

        public IOpinion Opinion { get; set; }
        public string CssClass { get; set; }
        public int CharsInOpinion { get; set; }
        public int CharsInPublisher { get; set; }

        public OpinionItem() {
            this.CharsInOpinion = 190;
        }

        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);

            if (this.Opinion != null) {
                this.SetControl();
            }
        }

        private void SetControl() {

            blackhouse.Aparts.Web.View.DisplayOpinion displayOpinion = new View.DisplayOpinion(this.Opinion);

            this.pnlOpinion.CssClass = this.CssClass;
            this.pnlOpinion.ID = "opinion-item-" + this.Opinion.Id;
            if (lblPersonCategory != null) {
                lblPersonCategory.Text = this.Opinion.Publisher.Category;
                lblPersonCategory.ToolTip = this.Opinion.Publisher.Category;
            }
            if (lblPersonName != null)
                lblPersonName.Text = this.Opinion.Publisher.FirstName + " " + this.Opinion.Publisher.LastName;

            if (lblCountry != null) {
                lblCountry.Text = displayOpinion.Country;
            }
            if (lblDate != null)
                lblDate.Text = displayOpinion.PublishedDateFormatted;
            if (lblInPlus != null)
                lblInPlus.Text = displayOpinion.InPlusOpinionFormatted;
            if (lblInMinus != null)
                lblInMinus.Text = displayOpinion.InMinusOpinionFormatted;
            if (lblScore != null)
                lblScore.Text = displayOpinion.AverageRatingFormatted;
            if (this.imgAvatar != null) {
                imgAvatar.ImageUrl = displayOpinion.AvatarImageUrl;
                imgAvatar.ToolTip = this.Opinion.Publisher.Category;
            }
            if (lnkToOpinionApartment != null) {
                this.lnkToOpinionApartment.NavigateUrl = displayOpinion.ApartmentNavigateUrl;
                this.lnkToOpinionApartment.Text = displayOpinion.ApartmentText;
                this.lnkToOpinionApartment.ToolTip = displayOpinion.ApartmentToolTip;
            }
            if (this.lnkToOpinion != null) {
                this.lnkToOpinion.NavigateUrl = displayOpinion.LinkToOpinion;
            }

            this.pnlNonPublic.Visible = !displayOpinion.IsPublic;

            if (displayOpinion.HaveAnswer) {
                this.pnlAnswer.Visible = true;
                this.ltlAnswer.Text = displayOpinion.Answer;
            }

        }


        public void SetAsTemplate() {
            this.pnlOpinion.CssClass = "gray-box-v2 " + this.CssClass;
            this.pnlOpinion.ID = "opinion-item-${Id}";
            if (lblPersonCategory != null) {
                lblPersonCategory.Text = "${Publisher.Category}";
                lblPersonCategory.ToolTip = "${Publisher.Category}";
            }
            if (lblPersonName != null)
                lblPersonName.Text = "${Publisher.FirstName} ${Publisher.LastName}";

            if (lblCountry != null) {

                lblCountry.Text = "${Country}";
            }
            if (lblDate != null)
                lblDate.Text = "${PublishedDateFormatted}";
            if (lblInPlus != null)
                lblInPlus.Text = "${InPlusOpinionFormatted}";
            if (lblInMinus != null)
                lblInMinus.Text = "${InMinusOpinionFormatted}";
            if (lblScore != null)
                lblScore.Text = "${AverageRatingFormatted}";
            if (this.imgAvatar != null) {
                imgAvatar.ImageUrl = "~/${AvatarImageUrl}";
                imgAvatar.ToolTip = "${Publisher.Category}";
            }
            if (this.lnkToOpinionApartment != null) {
                this.lnkToOpinionApartment.NavigateUrl = "~/${ApartmentNavigateUrl}";
                this.lnkToOpinionApartment.Text = "${ApartmentText}";
                this.lnkToOpinionApartment.ToolTip = "${ApartmentToolTip}";
            }
            if (this.lnkToOpinion != null) {
                this.lnkToOpinion.NavigateUrl = "~/${LinkToOpinion}";
            }

            this.ltlTmplNonPublicIf.Visible = true;
            this.ltlTmplNonPublicEndIf.Visible = true;
        }
    }
}